package thread.ABC;

public class ABCDemo2 {
    private static  int count = 0;

    public static void main(String[] args) throws InterruptedException {
        Object o=new Object();
        ABCDemo2 demo2=new ABCDemo2();
        Thread a = new Thread(demo2.new ABC(o));
        Thread b = new Thread(demo2.new ABC(o));
        Thread c = new Thread(demo2.new ABC(o));

        a.start();
        Thread.sleep(200);
        b.start();
        Thread.sleep(200);

        c.start();

    }
    class ABC implements Runnable{
        private Object o;

        public ABC(Object o) {
            this.o=o;
        }

        @Override
        public void run() {
                while(true){
                    synchronized (o){
                        while (count % 3 == 0) {
                            System.out.println(Thread.currentThread().getName() + "--" +"A");
                            count++;
                            try {
                                o.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        while (count % 3 == 1) {
                            System.out.println(Thread.currentThread().getName() + "--" + "B");
                            count++;
                            try {
                                o.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        while (count % 3 == 2) {
                            System.out.println(Thread.currentThread().getName() + "--" + "C");
                            System.out.println("------------");
                            count++;
                            try {
                                o.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }

                    }

                }

        }
    }

}

